<template>
  <div id="manageQuestionView">
    <a-table :columns="columns" :data="dataList" :pagination="true">
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)">修改</a-button>
          <a-popconfirm
            content="确定要删除吗?"
            position="tr"
            type="warning"
            @ok="doDelete(record)"
          >
            <a-button type="primary" status="danger">删除</a-button>
          </a-popconfirm>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { Modal } from "@arco-design/web-vue";
import { Question, QuestionControllerService } from "../../../api";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  current: 1,
  pageSize: 10,
});
const $modal = Modal;
const show = ref(true);
const columns = [
  {
    title: "ID",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    dataIndex: "content",
  },
  {
    title: "标签",
    dataIndex: "tags",
  },
  {
    title: "答案",
    dataIndex: "answer",
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "判题配置",
    dataIndex: "judgeConfig",
  },
  {
    title: "判题用例",
    dataIndex: "judgeCase",
  },
  {
    title: "创建用户id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
const router = useRouter();
/**
 * 修改
 * @param question
 */
const doUpdate = (question: Question) => {
  router.push({
    path: "/update/question",
    query: {
      update: " ",
      id: question.id,
    },
  });
};
/**
 * 删除
 * @param question
 */
const doDelete = async (question: Question) => {
  const rs = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (rs.code === 0) {
    message.success("删除成功");
    loadData();
  } else {
    message.error("删除失败,原因:" + rs.message);
  }
};

const loadData = async () => {
  const rs = await QuestionControllerService.listQuestionByPageUsingPost(
    searchParams.value
  );
  if (rs.code === 0) {
    dataList.value = rs.data.records;
    total.value = rs.data.total;
  } else {
    message.error("获取加载失败" + rs.message);
  }
};

onMounted(() => {
  loadData();
});
</script>

<style scoped></style>
